package com.demo.core.service;

import com.demo.base.model.UserInfo;
import com.demo.base.model.user.Actions;
import com.demo.base.model.user.Roles;
import com.demo.core.dao.ActionsDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiandandia on 2017/4/7.
 */
@Transactional
@Service
public class ActionsService {
    @Autowired
    private ActionsDao actionsDao;

    public Roles saveRole(Roles roles) throws Exception{
        return actionsDao.saveRole(roles);
    }

    public void deleteRoleById(String roleId) throws Exception{
        String[] roleIds = StringUtils.split(roleId, ',');
        for (String rid : roleIds) {
            actionsDao.deleteRoleById(rid);
        }
    }

    public List<Actions> getRoleActions(String roleId){
        return actionsDao.getRoleActions(roleId);
    }

    public void saveRoleActions(String roleId, String[] actionIds) throws Exception {
        List<Actions> actions = actionsDao.getRoleActions(roleId);
        List<String> roleActions = new ArrayList<String>(actions.size());
        List<String> newActions = new ArrayList<String>();
        List<String> updActions = new ArrayList<String>();
        List<String> delActions = new ArrayList<String>();
        for (Actions a : actions) {
            roleActions.add(a.getId());
        }
        for (String actionId : actionIds) {
            if (roleActions.contains(actionId))
                updActions.add(actionId);
            else
                newActions.add(actionId);
        }
        for (String actionId : roleActions) {
            if (!updActions.contains(actionId))
                delActions.add(actionId);
        }
        try {
            if (!newActions.isEmpty())
                actionsDao.saveRoleActions(roleId, newActions.toArray(new String[]{}));
            if (!delActions.isEmpty())
                actionsDao.deleteRoleActions(roleId, delActions.toArray(new String[]{}));
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public void newRoleUsers(String roleId, String[] userIds) throws Exception{
        List<UserInfo> userRoles = actionsDao.getRoleUsers(roleId);
        try {
            if (CollectionUtils.isEmpty(userRoles)) {
                actionsDao.newRoleUsers(roleId, userIds);
            } else {
                List<String> uniqUserIds = new ArrayList<String>();
                List<String> users = new ArrayList<String>(userRoles.size());
                for (UserInfo r : userRoles) {
                    users.add(r.getId());
                }
                for (String userId : userIds) {
                    if (!users.contains(userId))
                        uniqUserIds.add(userId);
                }
                if (!uniqUserIds.isEmpty())
                    actionsDao.newRoleUsers(roleId, uniqUserIds.toArray(new String[]{}));
            }
        }catch(Exception e){
            throw new Exception(e);
        }
    }

    public void deleteRoleUsers(String roleId, String[] userIds){
        actionsDao.deleteRoleUsers(roleId, userIds);
    }
}
